# _*_ coding: utf-8 _*_
"""
@ 时间    ：2024/10/24 9:31
@ 作者    ：旺财
@ 文件    ：01 AdaBoost算法.py
@ 说明    ：信用卡精准营销模型
"""

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score, roc_curve


# 1.读取数据
df = pd.read_excel('信用卡精准营销模型.xlsx')
df.rename(columns={'响应': '是否办卡'}, inplace=True)
print(df.head())

# 2.特征变量与目标变量提取
x = df.drop(columns='是否办卡')
y = df['是否办卡']

# 3.划分测试集与训练集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=123)

# 4.搭建模型
mode = AdaBoostClassifier(random_state=123, algorithm="SAMME")
mode.fit(x_train, y_train)

# 5.模型评估
# 5.1 打印预测结果
df_score = pd.DataFrame()
df_score['预测值'] = list(mode.predict(x_test))
df_score['实际值'] = list(y_test)
print(df_score.head())

# 5.2 打印准确率
score = mode.score(x_test, y_test)
print(f'准确率为:{round(score*100, 2)}%')

# 5.3 打印AUC值
y_proba = mode.predict_proba(x_test)[:, 1]
auc_score = roc_auc_score(y_test, y_proba)
print(f'AUC值为:{round(auc_score*100, 2)}%')

# 6.可视化-绘制ROC曲线
fpr, tpr, _ = roc_curve(y_test, y_proba)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.plot(fpr,tpr)
plt.title('ROC曲线')
plt.xlabel('误报率')
plt.ylabel('命中率')
plt.show()